//
//  FZIntroduceViewController.m
//  EnglishTalk
//
//  Created by CyonLeuPro on 15/8/24.
//  Copyright (c) 2015年 Feizhu Tech. All rights reserved.
//

#import "FZIntroduceViewController.h"
#import "FZIntroduceCollectionViewCell.h"
#import "SRCommonConstants.h"
#import "UIButton+FZGreenButton.h"
#import <UIDeviceUtil.h>

@interface FZIntroduceViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) UIScrollView *backgroundScrollView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionViewFlowlayout;
//@property (weak, nonatomic) IBOutlet UIView *loginBgView;
//@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (strong, nonatomic) UIButton *skipButton;

//@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginbgHeightConstraint;
@property (weak, nonatomic) IBOutlet UIPageControl      *pageControl;

@property (strong, nonatomic) NSArray *items;

//@property (strong, nonatomic) NSArray *titleItems;
//
//@property (strong, nonatomic) NSArray *animationItems;
@property (assign, nonatomic)  CGFloat imageOffsetRatio;//背景图可以移动的宽度比例
//@property (assign, nonatomic)  CGFloat imageOffsetWidth;//背景图可以移动的宽度



@end

@implementation FZIntroduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupData];
    [self setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupView {
    
//    self.loginbgHeightConstraint.constant = 0;
//    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    self.view.backgroundColor = [UIColor whiteColor];
    
        //背景图
    UIImageView *bgImageView = [[UIImageView alloc] init];
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    UIImage *bgImage = nil;
    if ([UIDeviceUtil hardware] == IPHONE_4 || [UIDeviceUtil hardware] == IPHONE_4S) {
        bgImage = [UIImage imageNamed:@"guide_page_BG_640"];
    } else {
        bgImage = [UIImage imageNamed:@"guide_page_BG"];
    }
    
    bgImageView.image = bgImage;
    CGSize imageSize = bgImage.size;
    imageSize.height = SCREEN_HEIGHT;
    imageSize.width = SCREEN_HEIGHT * bgImage.size.width / bgImage.size.height;
    
    CGFloat imageOffsetWidth = imageSize.width - SCREEN_WIDTH;
    CGFloat cellOffsetWidth = SCREEN_WIDTH * ([self.items count] - 1);
    self.imageOffsetRatio = imageOffsetWidth / cellOffsetWidth;

    
    self.backgroundScrollView = [[UIScrollView alloc] init];
    [self.backgroundScrollView addSubview:bgImageView];
    
    
    bgImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
    self.backgroundScrollView.contentSize = bgImage.size;
    
    [self.view addSubview:self.backgroundScrollView];
    [self.backgroundScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view sendSubviewToBack:self.backgroundScrollView];

    UIView *collectionBgView = [[UIView alloc] init];
    collectionBgView.backgroundColor = [UIColor clearColor];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.backgroundView = collectionBgView;
    
    [self.collectionView registerNib:[UINib nibWithNibName:kFZIntroduceCollectionViewCell bundle:nil] forCellWithReuseIdentifier:kFZIntroduceCollectionViewCell];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    CGFloat itemSpace = 0.f;
    CGFloat itemWidth = SCREEN_WIDTH - itemSpace * 1;
    CGFloat itemHeight = SCREEN_HEIGHT;
    self.collectionViewFlowlayout.itemSize = CGSizeMake(itemWidth, itemHeight);
    self.collectionViewFlowlayout.minimumInteritemSpacing = itemSpace;
    self.collectionViewFlowlayout.minimumLineSpacing = itemSpace;
    
//    [self.loginButton applyGreenBackgroundColor];
//    [self.loginButton addTarget:self action:@selector(onLoginButton:) forControlEvents:UIControlEventTouchUpInside];
//    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithHex:0x10dd96];
    
        //跳过按钮
    
    self.skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.skipButton setImage:[UIImage imageNamed:@"guide-page_button_skip"] forState:UIControlStateNormal];
    [self.view addSubview:self.skipButton];
    [self.skipButton addTarget:self action:@selector(onSkipButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(24);
        make.trailing.equalTo(self.view).offset(-24);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    [self.view bringSubviewToFront:self.skipButton];
}



- (void)setupData {
    if ([UIDeviceUtil hardware] == IPHONE_4 || [UIDeviceUtil hardware] == IPHONE_4S) {
        self.items = @[@"guide_page_1_640", @"guide_page_2_640", @"guide_page_3_640"];
    } else {
        self.items = @[@"guide_page_1", @"guide_page_2", @"guide_page_3"];
    }
//    self.items = @[@"guide_page_1", @"guide_page_2", @"guide_page_3"];
    
    self.pageControl.numberOfPages = self.items.count;
   
//    self.animationItems = @[@"guide_page_1", @"guide_page_2", @"guide_page_3"];
//    
//    self.titleItems = @[@"guide_1_title", @"guide_2_title", @"guide_3_title"];
}


#pragma mark - Event 

- (void)onLoginButton:(UIButton *)sender {
    [self hideIntroduceAnimation];
}

- (void)hideIntroduceAnimation {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFirstInstalIntroduceDidShow];
    WEAKSELF
    [UIView animateWithDuration:0.3 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        if (weakSelf.didInstroduceBlock) {
            weakSelf.didInstroduceBlock();
        }
    }];
}

- (void)onSkipButtonAction:(id)sender {
    [self hideIntroduceAnimation];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -  UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.items count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FZIntroduceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFZIntroduceCollectionViewCell forIndexPath:indexPath];
    cell.imageView.image =  [UIImage imageNamed:self.items[indexPath.row]];
//    [cell.animationImageView layoutIfNeeded];
//    [cell resetAnimationImageFrame];
    
    WEAKSELF
    cell.entranceButtonBlock = ^(void) {
        [weakSelf hideIntroduceAnimation];
    };
 
    if (indexPath.item == ([self.items count] - 1)) {
        //最后一个显示开启按钮
        cell.entranceButton.hidden = NO;
    } else {
        cell.entranceButton.hidden = YES;
    }
    return cell;
}

#pragma mark - UICollectionViewDelegate 


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == ([self.items count] - 1)) {
        //最后一个可点击进入
        [self hideIntroduceAnimation];
    }
}
//- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
//{
//}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
//    NSLog(@"*****scrollViewDidScroll:%@", NSStringFromCGPoint(scrollView.contentOffset));
    
    CGFloat maxOffsetX = CGRectGetWidth(self.collectionView.frame) * (self.items.count - 1) + 40;
    int currentPage = (self.collectionView.contentOffset.x + 1.f)/ self.collectionView.frame.size.width ;
    if (currentPage == (self.items.count - 1)) {
            //最后一行隐藏pagecontrol
        self.pageControl.hidden = YES;
    } else {
        self.pageControl.hidden = NO;
    }
    self.pageControl.currentPage = currentPage;
    if (scrollView.contentOffset.x >= maxOffsetX) {
        [self hideIntroduceAnimation];
    }

    CGPoint bgOffset;
    bgOffset.x = scrollView.contentOffset.x * self.imageOffsetRatio;
    bgOffset.y = 0;
//    NSLog(@"*****bgOffset:%@", NSStringFromCGPoint(bgOffset));

    [self.backgroundScrollView setContentOffset:bgOffset];
}

@end
